import arcpy
import os
from arcpy import env
import sys
reload(sys)
sys.setdefaultencoding('utf8')

try:
    arcpy.AddMessage('-----------start-----------')
    inputShp=arcpy.GetParameterAsText(0)
    inputImage=arcpy.GetParameterAsText(1)
    resultPathTif=arcpy.GetParameterAsText(2)
    arcpy.AddMessage(inputShp)
    arcpy.AddMessage(inputImage)
    arcpy.AddMessage(resultPathTif)
    n=0
    with arcpy.da.SearchCursor(inputShp, ["SHAPE@",'NAME']) as cursor:
        for row in cursor:
            mask = row[0]
            out_name=str(row[1])+'.tif'
            extent = str(mask.extent.XMin) + " " + str(mask.extent.YMin) + " " + str(mask.extent.XMax) + " " + str(mask.extent.YMax)
            outPath=os.path.join(resultPathTif,out_name)
            arcpy.AddMessage(outPath)
            arcpy.Clip_management(inputImage, extent, outPath, mask, "0", "ClippingGeometry")
            n+=1
    arcpy.AddMessage('All completed :'+str(n))
    arcpy.AddMessage('-----------end-----------')
except arcpy.ExecuteError:
    print arcpy.GetMessages()
    